package geo.campus;
// package fr.univ_orleans.cri.android.geo.campus;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseAdaptateur {

	//modifier la version de la base en ajoutant +1
	//à la valeur courante pour que la BD prennent
	//en compte la modification du fichier d'insertion
	//des données
	private static final int BASE_VERSION = 12;
	private static final String BASE_NOM = "campus.db";


	private SQLiteDatabase mabd;
	private Database database;

	public DatabaseAdaptateur(Context ctx)
	{
		database = new Database(ctx, BASE_NOM, null, BASE_VERSION);
	}


	//rend la base de données lisible
	public DatabaseAdaptateur open()
	{
		mabd = database.getWritableDatabase();
		return this;
	}

	//ferme la base de données
	public void close()
	{
		mabd.close();
	} 

	//retourne la base de données
	public SQLiteDatabase getBaseDonnees()
	{
		return mabd;
	}


	//insertion des données dans la base
	public long insertBatiment(batiment b)
	{
		ContentValues valeurs= new ContentValues();
		valeurs.put("nom", b.getNom());
		valeurs.put("telephone", b.getTelephone());
		valeurs.put("adresse", b.getAdresse());
		valeurs.put("latitude", b.getLatitude());
		valeurs.put("longitude", b.getLongitude());
		valeurs.put("photo", b.getPhoto());
		valeurs.put("categorieid", b.getCat());
		valeurs.put("zone", b.getZone());
		return mabd.insert(BASE_NOM, null, valeurs);
	}

	public long insertBatiment(ContentValues v)
	{
		return mabd.insert(BASE_NOM, null, v);
	}
	
	public void insertsql(String insert)
	{
		mabd.execSQL(insert);
	}

	public Cursor selectallbat()
	{
		Cursor c = mabd.rawQuery("SELECT nom FROM BATIMENT", null);
		return c;
	}
	
	public Cursor selectallcampus()
	{
		Cursor c = mabd.rawQuery("SELECT nom FROM CAMPUS", null);
		return c;
	}
	
	public Cursor selectallcat()
	{
		return mabd.rawQuery("SELECT nom FROM CATEGORIE", null);
	}
	
	public Cursor selectallzone()
	{
		return mabd.rawQuery("SELECT nom FROM ZONES", null);
	}
	
	public Cursor selectbycat(String nomcat)
	{
		String[] cat = new String[1];
		cat[0] = nomcat;
		Cursor c = mabd.rawQuery("SELECT catid FROM CATEGORIE WHERE nom=?", cat);
		Cursor res = null;
		String[] cat2 = new String[1];
		if(c.getCount() != 0)
		{
			c.moveToFirst();
			Integer id = c.getInt(0);
			cat2[0] = id.toString();
			c.close();
			res = mabd.rawQuery("SELECT nom FROM BATIMENT WHERE categorieid=?", cat2);
		}
		return res;
	}
	
	public Cursor selectbyzone(String zone)
	{
		String[] zon = new String[1];
		zon[0] = zone;
		Cursor c = mabd.rawQuery("SELECT zoneid FROM ZONES WHERE nom=?", zon);
		Cursor res = null;
		String[] zon2 = new String[1];
		if(c.getCount() != 0)
		{
			c.moveToFirst();
			Integer id = c.getInt(0);
			zon2[0] = id.toString();
			c.close();
			res = mabd.rawQuery("SELECT nom FROM BATIMENT WHERE zoneid=?", zon2);
		}
		return res;
	}
	
	public Cursor selectbycatandid(String nomcat, String zone)
	{
		String[] cat = new String[1];
		cat[0] = nomcat;
		Cursor c = mabd.rawQuery("SELECT catid FROM CATEGORIE WHERE nom=?", cat);
		String[] zon = new String[1];
		zon[0] = zone;
		Cursor c1 = mabd.rawQuery("SELECT zoneid FROM ZONES WHERE nom=?", zon);
		Cursor res = null;
		String[] catzone = new String[2];
		if(c.getCount() != 0 && c1.getCount() != 0)
		{
			c.moveToFirst();
			c1.moveToFirst();
			Integer id = c.getInt(0);
			Integer id1 = c1.getInt(0);
			catzone[0] = id.toString();
			catzone[1] = id1.toString();
			c.close();
			c1.close();
			res = mabd.rawQuery("SELECT a.nom FROM BATIMENT a WHERE a.categorieid=? AND a.zoneid=?", catzone);
		}
		return res;
	}
	
	public Cursor selectbynom(String nom)
	{
		Cursor res = null;
		String[] inter = new String[1];
		inter[0] = nom;
		res=mabd.rawQuery("SELECT adresse, latitude, longitude FROM BATIMENT WHERE nom=?", inter);
		return res;
	}
	
	public Cursor selectinfores(String nom)
	{
		Cursor res = null;
		String[] inter = new String[1];
		inter[0] = nom;
		res = mabd.rawQuery("SELECT telephone, photo FROM BATIMENT WHERE nom=?", inter);
		return res;
	}
	
	public Cursor selectbycat2(String zone)
	{
		String[] zon = new String[1];
		zon[0] = zone;
		Cursor c = mabd.rawQuery("SELECT catid FROM CATEGORIE WHERE nom=?", zon);
		Cursor res = null;
		String[] zon2 = new String[1];
		if(c.getCount() != 0)
		{
			c.moveToFirst();
			Integer id = c.getInt(0);
			zon2[0] = id.toString();
			c.close();
			res = mabd.rawQuery("SELECT nom, latitude, longitude FROM BATIMENT WHERE categorieid=?", zon2);
		}
		return res;
	}
}
